<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/
include_once($semf_root.'page_designer/components/Component.php');

class MenuComponent extends Component
{
	public static function get_properties()
	{
		return array('Apearance'=> array(
							'Root'=> '-'),
					'Links'=> array(
							'Root'=> 'Link for Root')
					 );	
	}

	
	/** build properties dynamically if atmost one property is changed*/
	public static function build_properties($parent_container, $properties, $property_group, $key, $value)
	{
		if( $key != NULL )
		{
			$properties[$property_group][$key] = $value;
			//some big modification have to be carry on..
			
			$new_properties = array();
			$current_menu = "Root"; //start from the top
			
			$properties = MenuComponent::build_menu($current_menu, $new_properties, $properties);
		}
		return 	$properties;
	}
	
	private static function build_menu($current_menu, $properties, $old_properties)
	{
		$current_menu_value = $old_properties["Apearance"][$current_menu];
		$comma_pos = strpos($current_menu_value, ",");
		if($comma_pos === false)
		{
			$comma_pos = strlen($current_menu_value);
		}
		$tok = substr($current_menu_value, 0, $comma_pos);
		$rest = substr($current_menu_value, $comma_pos + 1);
		
		//$tok = strtok($current_menu_value, ",");
		if($tok == NULL || $tok == "" || $tok == "-") //no sub menu exists so fill it in the link
		{
			$old_link_value = $old_properties["Links"][$current_menu];
			if($old_link_value == NULL)
			{
				$old_link_value = "'Link for ".$current_menu."'";
			}
			$properties["Apearance"][$current_menu] = "-";
			$properties["Links"][$current_menu] = $old_link_value;
			//so it s over for that menu..
		}
		else
		{
			//now sub menu exists..
			$properties["Apearance"][$current_menu] = $current_menu_value;
			do
			{
				$sub_menu_name = $current_menu."->".$tok;
				$menu_old_value = $old_properties["Apearance"][$sub_menu_name];
				if($menu_old_value == NULL)
				{
					$menu_old_value = "-";
				}
				$properties["Apearance"][$sub_menu_name] = $menu_old_value;
				
				$properties = MenuComponent::build_menu($sub_menu_name, $properties, $old_properties);

				$comma_pos = strpos($rest, ",");
				if($comma_pos === false)
				{
					$comma_pos = strlen($rest);
				}
				$tok = substr($rest, 0, $comma_pos);
				$rest = substr($rest, $comma_pos + 1);
				//$tok = strtok(",");
			}
			while($tok != NULL);
		}
		return $properties;
	}
	
	public static function build_containers( $parent_container, $properties)
	{
		//we do not have any container for text box...
		return array( );
	}
	
	/*
	public static function get_action_info($component_id)
	{
		return array('onClick'=> array(
							'action' => action::$CLICK,
							'elem1' => "btn_".$component_id,));
	}
	*/
	
	public static function get_link_info($component_id, $properties)
	{
		$link_keys = array_keys($properties);
		$ret_array = array();
		
		foreach($link_keys as $link_key)
		{
			$temp_array = array($link_key => array(
							'attri' => 'value',
							'databind' => "menu_".$component_id."_link_".$link_key));
			$ret_array = array_merge($ret_array, $temp_array);
		}
		return $ret_array;
	}

}
?>